package com.wl.application.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.wl.application.common.all.Const;
import com.wl.application.common.all.Result;
import com.wl.application.entity.Role;
import com.wl.application.services.AllotService;
import com.wl.application.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/21
 * @description:
 */
@RestController
@RequestMapping("/role")

public class RoleController {

    @Autowired
    private RoleService roleService;


    @Autowired
    private AllotService allotService;

    @GetMapping("/list")
    public Result list() {
        return Result.ok(roleService.list());
    }


    @PostMapping("/page")
    public Result page(@RequestBody Page<Role> page) {
        return Result.ok(roleService.page(page));
    }

    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        role.setRstate(Const.NO_ACTIVATE);
        role.setCreateTime(new Date());
        return Result.ok("添加成功!", roleService.save(role));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        return Result.ok("修改成功!", roleService.updateById(role));
    }

    @DeleteMapping("/del")
    public Result delete(@RequestBody List<Integer> list) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("rid", list.get(0));
        allotService.remove(wrapper);
        allotService.removeBatchByIds(list);
        return Result.ok("删除成功!", roleService.removeBatchByIds(list));
    }

    @PutMapping("/update/state/{id}")
    public Result updateState(@PathVariable("id") Integer id) {
        Role byId = roleService.getById(id);
        if (byId.getRstate().equals(Const.NO_ACTIVATE)) {
            byId.setRstate(Const.ACTIVATE);
        } else {
            byId.setRstate(Const.NO_ACTIVATE);
        }
        return Result.ok(roleService.updateById(byId));
    }

    @GetMapping("/one/{id}")
    public Result getOne(@PathVariable("id") Integer id) {
        return Result.ok(roleService.getById(id));
    }
}
